package com.fastwire.carriere.common.mapper;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.dao.DAOException;
import com.fastwire.carriere.inventory.entity.Equipment;
import com.fastwire.carriere.inventory.entity.NetworkElement;
import com.fastwire.carriere.inventory.entity.TerminationPoint;

// TODO: Auto-generated Javadoc
/**
 * The Class TPNameMapper.
 */
public class TPNameMapper {

	/**
	 * Gets the readable ctp name.
	 * 
	 * @param tpName
	 *            the tp name
	 * @param networkElement
	 *            the network element
	 * @return the readable ctp name
	 */
	public static String getReadableCTPName(String tpName,
			NetworkElement networkElement) {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}
		Pattern cmdPattern = Pattern
				.compile(".*ME=(\\d+)::.*slot=(\\d+).*port=(\\d+).*CTP=/(.*)");
		Matcher matcher = cmdPattern.matcher(tpName);

		String slot = "";
		String port = "";
		String ctp = "";
		String card = "";

		if (matcher.matches()) {

			slot = matcher.group(2);
			port = matcher.group(3);
			ctp = matcher.group(4);

			Set<Equipment> equipments = networkElement.getEquipments();
			if (equipments != null) {
				for (Equipment eq : equipments) {
					if (slot.equals(eq.getSlotId())
						&& !eq.getEmsNativeName().startsWith("Slot")  // added on 20120418
						) {
						card = eq.getEmsNativeName();
					}
				}
			}
			return slot + "-" + card + "-" + port + "/" + ctp;

		} else {
			return "";
		}
	}

	/**
	 * Gets the readable ptp name.
	 * 
	 * @param tpName
	 *            the tp name
	 * @param networkElement
	 *            the network element
	 * @return the readable ptp name
	 */
	public static String getReadablePTPName(String tpName,
			NetworkElement networkElement) {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}
		Pattern cmdPattern = Pattern
				.compile(".*ME=(\\d+)::.*slot=(\\d+).*/port=(\\d+)");
		Matcher matcher = cmdPattern.matcher(tpName);

		String slot = "";
		String port = "";
		String card = "";

		if (matcher.matches()) {

			slot = matcher.group(2);
			port = matcher.group(3);
			if (networkElement != null) {
				Set<Equipment> equipments = networkElement.getEquipments();
				if (equipments != null) {
					for (Equipment eq : equipments) {
						if (!StringUtil.isEmptyString(eq.getSlotId())
								&& eq.getSlotId().equals(slot)
								&& !eq.getEmsNativeName().startsWith("Slot")) {
							card = eq.getEmsNativeName();
						}
					}
				}
				return slot + "-" + card + "-" + port;
			}
		}
		return "";

	}

	/**
	 * Gets the readable ftp name.
	 * 
	 * @param tpName
	 *            the tp name
	 * @param networkElement
	 *            the network element
	 * @return the readable ftp name
	 */
	public static String getReadableFTPName(String tpName,
			NetworkElement networkElement) {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}

		Pattern pattern = Pattern.compile(".*(FTP=.*)");
		Matcher matcher = pattern.matcher(tpName);

		if (matcher.matches()) {
			return matcher.group(1);
		} else {
			return "";
		}
	}

	/**
	 * Gets the readable tp name.
	 * 
	 * @param tpName
	 *            the tp name
	 * @param type
	 *            the type
	 * @param ne
	 *            the ne
	 * @return the readable tp name
	 * @throws DAOException
	 *             the dAO exception
	 */
	public static String getReadableTpName(String tpName, String type,
			NetworkElement ne) throws DAOException {

		if (ne == null) {
			String neId = NENameMapper.getNEIdbyName(tpName);
			ne = NENameMapper.getNEbyName(neId);
		}

		if (AppConstants.PTP.equals(type)) {
			return getReadablePTPName(tpName, ne);
		} else if (AppConstants.CTP.equals(type)) {
			return getReadableCTPName(tpName, ne);
		} else if (AppConstants.FTP.equals(type)) {
			return getReadableFTPName(tpName, ne);
		}

		return "";

	}

	/**
	 * Gets the unique keyfor tp.
	 * 
	 * @param tpName
	 *            the tp name
	 * @param ne
	 *            the ne
	 * @return the unique keyfor tp
	 * @throws DAOException
	 *             the dAO exception
	 */
	public static String getUniqueKeyforTP(String tpName, NetworkElement ne)
			throws DAOException {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}
		Pattern cmdPattern = Pattern
				.compile(".*ME=(\\d+)::.*slot=(\\d+).*port=(\\d+).*CTP=/(.*)");
		Matcher matcher = cmdPattern.matcher(tpName);
		String neId = "";
		String slot = "";
		String port = "";

		if (matcher.matches()) {
			neId = matcher.group(1);
			slot = matcher.group(2);
			port = matcher.group(3);

			return neId + "-" + slot + "-" + getCardName(ne, slot) + "-" + port;

		} else {
			return "";
		}
	}

	/**
	 * Gets the card name.
	 * 
	 * @param networkElement
	 *            the network element
	 * @param slot
	 *            the slot
	 * @return the card name
	 * @throws DAOException
	 *             the dAO exception
	 */
	public static String getCardName(NetworkElement networkElement, String slot)
			throws DAOException {

		String card = "";

		if (networkElement != null) {
			Set<Equipment> equipments = networkElement.getEquipments();
			for (Equipment eq : equipments) {
				if (eq != null && !StringUtil.isEmptyString(eq.getSlotId())
						&& eq.getSlotId().equals(slot)
						&& !StringUtil.isEmptyString(eq.getEquipmentType())) {
					card = eq.getEmsNativeName();
					break;
				}
			}

		}
		return card;
	}

	/**
	 * Gets the cTP label.
	 * 
	 * @param tpName
	 *            the tp name
	 * @return the cTP label
	 */
	public static String getCTPLabel(String tpName) {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}
		String label = "";
		Pattern cmdPattern = Pattern.compile(".*CTP=.*Label=(\\d+)");
		Matcher matcher = cmdPattern.matcher(tpName);
		if (matcher.matches()) {
			label = matcher.group(1);
		}

		return label;

	}

	/**
	 * Gets the tP hash key.
	 * 
	 * @param tpName
	 *            the tp name
	 * @return the tP hash key
	 * @throws DAOException
	 *             the dAO exception
	 */
	public static String getTPHashKey(String tpName) throws DAOException {

		if (StringUtil.isEmptyString(tpName)) {
			return "";
		}
		Pattern cmdPattern = Pattern
				.compile(".*ME=(\\d+)::.*slot=(\\d+).*/port=(\\d+).*");
		Matcher matcher = cmdPattern.matcher(tpName);
		String slot = "";
		String port = "";

		if (matcher.matches()) {
			slot = matcher.group(2);
			port = matcher.group(3);
			return generateKey(slot, port);

		}

		else {
			return "";
		}

	}

	/**
	 * Generate key.
	 * 
	 * @param slot
	 *            the slot
	 * @param port
	 *            the port
	 * @return the string
	 */
	public static String generateKey(String slot, String port) {
		return slot + "-" + port;
	}

	/**
	 * Parses the u2000 ptp name.
	 * 
	 * @param tp
	 *            the tp
	 */
	public static void parseU2000PTPName(TerminationPoint tp) {

		Pattern cmdPattern = Pattern
				.compile(".*ME=(\\d+).*PTP=.*/slot=(\\d+)/domain=(.*)/port=(\\d+)");
		Matcher matcher = cmdPattern.matcher(tp.getName());
		if (matcher.matches()) {

			tp.setNeName(matcher.group(1));
			tp.setSlot(matcher.group(2));

			tp.setPort(matcher.group(4));
		} else {
			Pattern cmdPattern2 = Pattern
					.compile(".*ME=(\\d+).*PTP=.*/slot=(\\d+)/port=(\\d+)");

			Matcher matcher2 = cmdPattern2.matcher(tp.getName());
			if (matcher2.matches()) {

				tp.setNeName(matcher2.group(1));
				tp.setSlot(matcher2.group(2));
				tp.setPort(matcher2.group(3));
			} else {
				Pattern cmdPattern3 = Pattern
						.compile(".*ME=(\\d+).*PTP=.*/slot=(\\d+)/sub_slot=(\\d+)/domain=(.*)/port=(\\d+)");

				Matcher matcher3 = cmdPattern3.matcher(tp.getName());
				if (matcher3.matches()) {

					tp.setNeName(matcher3.group(1));

					tp.setSlot(matcher3.group(2));
					tp.setSubSlot(matcher3.group(3));
					tp.setPort(matcher3.group(5));
				} else {
					CommonLogger.warn(TPNameMapper.class, "Cannot find matching rule for parsing PTP: "
							+ tp.getName());
					//System.err.println("Cannot find matching rule"+ tp.getName());
				}
			}

		}

	}

}
